<template>
	<view class="page">
		<view class="item">
			<text>头像</text>
			<!-- <image :src="$wanlshop.oss(form.avatar)" mode="aspectFill"></image> -->
			
			<button class="acvter_all" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"
				style="display: flex;align-items: center;flex: 1;justify-content: flex-end;">
				<view v-if="form.avatar" class="img">
					<image :src="$wanlshop.oss(form.avatar)"></image>
				</view>
				<view v-else class="mast">请选择头像</view>
			</button>
		</view>
		<view class="item" v-if="form.nickname">
			<text>昵称</text>
			<input type="nickname" placeholder="请输入" @blur="bindblur"  :value="form.nickname" />
		</view>
		<view class="item" @tap="$wanlshop.to('/pages/restaurant/user/user/bind')">
			<text>手机号</text>
			<input type="text" placeholder="绑定手机号" v-model="form.username" disabled />
			<image src="/static/restaurant/user/right.png" mode="widthFix"></image>
		</view>
		<view class="item">
			<text>性别</text>
			<picker @change="bindPickerChange" :value="form.gender" :range="array">
				<view class="uni-input">{{array[form.gender]}}</view>
			</picker>
		</view>
		<view class="item" @tap="tap">
			<text>实名信息</text>
			<image src="/static/restaurant/user/right.png" mode="widthFix"></image>
		</view>
		<!-- <view class="item">
			<text>证件号码</text>
			<input type="text" placeholder="请输入" v-model="form.identity" disabled />
		</view> -->
		<!-- <view class="item">
			<text>家庭住址</text>
			<image src="/static/restaurant/user/right.png" mode="widthFix"></image>
		</view>
		<view class="item">
			<text>年龄</text>
			<input type="text" placeholder="请输入" />
		</view> -->

		<!-- <view class="upload">
			<text>人脸照片</text>
			<view class="up">
				<text>+</text>
				<text>上传图片</text>
			</view>
		</view> -->

		<view class="btn" @tap="submit">
			提交
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				array: ['保密', '男', '女'],
				form: {
					nickname:'',
					avatar: '',
					username: '',
					gender: 0, // 0 保密 1 男 2 女
					identity: ''
				}
			}
		},
		computed: {
			...mapState(['user'])
		},
		onShow() {
			this.form.avatar = this.user.avatar;
			this.form.nickname = this.user.nickname;
			this.form.username = this.user.username;
			this.form.gender = this.user.gender;
		},
		methods: {
			bindblur(e) {
				console.log(e)
				this.form.nickname = e.detail.value
			},
			async getUserInfo() {
				let res = await this.$api.getUserInfo();
				if (res.code == 1) {
					this.$store.dispatch('user/login', res.data);
				}
			},
			bindPickerChange: function(e) {
				this.form.gender = e.detail.value
			},
			tap() {
				if(this.user.apply_status == 0 || this.user.apply_status == 3) {
					this.$wanlshop.auth('/pages/restaurant/user/user/idcard')
				} else {
					this.$wanlshop.auth('/pages/restaurant/user/user/realname')
				}
			},
			async submit() {
				let formData = {
					avatar: this.form.avatar,
					nickname: this.form.nickname,
					gender: this.form.gender
				}
				let res = await this.$api.updateProfile(formData);
				if (res.code == 1) {
					this.$wanlshop.msg(res.msg)
					setTimeout(() => {
						uni.navigateBack()
					}, 1500);
				}
			},
			onChooseAvatar(e) {
				let that = this
				console.log(e.detail)
				uni.uploadFile({
					url: `${that.$config.baseUrl}/upfile/image`,
					filePath: e.detail.avatarUrl,
					name: 'file',
					success: (uploadFileRes) => {
						if (JSON.parse(uploadFileRes.data).code == 1) {
							that.form.avatar = JSON.parse(uploadFileRes.data).data.path;
						}
					}
				});
			},
			
			upload() {
				let token = uni.getStorageSync("token")
				let that = this
				uni.chooseImage({
					sizeType: ['original'],
					count: 1,
					async success(res){
						uni.uploadFile({
							url: `${that.$config.baseUrl}/upfile/image`,
							filePath: res.tempFilePaths[0],
							header: {token: token},
							name: 'file',
							success: (uploadFileRes) => {
								if(JSON.parse(uploadFileRes.data).code == 1) {
									that.form.avatar = JSON.parse(uploadFileRes.data).data.path;
								}
							}
						});
					}
				})
			},
		}
	}
</script>
<style lang="less">
	page {
		background-color: #fff;
	}
</style>
<style lang="less" scoped>
	.page {
		padding: 20rpx;
	}

	.switch-sex {
		transform: scale(0.7);
		margin-right: -20rpx;
	}

	.item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
		padding-left: 16rpx;
		border-bottom: 1px solid #E0E0E0;

		text {
			color: #333;
			font-size: 32rpx;
		}

		image {
			width: 48rpx;
			height: 48rpx;
		}

		input {
			flex: 1;
			text-align: right;
		}

		&:nth-child(1) {
			image {
				width: 72rpx;
				height: 72rpx;
				border-radius: 50%;
			}
		}
	}

	.upload {
		padding-left: 16rpx;

		>text {
			color: #333;
			display: inline-block;
			height: 100rpx;
			line-height: 100rpx;
			font-size: 32rpx;
		}

		.up {
			width: 174rpx;
			height: 174rpx;
			border: 1px dashed #999999;
			display: flex;
			flex-direction: column;
			align-items: center;

			text {
				color: #999999;

				&:nth-child(1) {
					font-size: 70rpx;
				}

				&:nth-child(2) {
					font-size: 28rpx;
				}
			}
		}
	}

	.btn {
		width: 510rpx;
		height: 84rpx;
		background: #FA802A;
		border-radius: 42rpx 42rpx 42rpx 42rpx;
		// margin: 200rpx auto 0;
		line-height: 84rpx;
		text-align: center;
		color: #fff;
		font-size: 36rpx;
		font-weight: 500;
		position: fixed;
		bottom: 300rpx;
		left: 50%;
		margin-left: -255rpx;
	}
</style>